/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-13 23:44:25
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-08-27 22:31:47
 */

import { dictProvincePage, dictProvinceAddOrEdit, dictProvinceDelete } from 'services/dict';

export default {
  namespace: 'provinceList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(dictProvincePage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },
    *dictProvinceAddOrEdit({ payload, callback }, { call, put }) {
      const response = yield call(dictProvinceAddOrEdit, payload);
      callback(response);
    },
    *dictProvinceDelete({ payload, callback }, { call, put }) {
      const response = yield call(dictProvinceDelete, payload);
      callback(response);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { provinceList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = provinceList.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
  },
};
